// app.js
import { getLoginCode, sendCodeToServer, checkToken, checkSession } from "./service/api_login"
import { TOKEN_KEY } from "./constants/token-const"

App({
  globalData: {
    screenWidth: 0,
    screenHeight: 0,
    statusBarHeight: 0,
    navBarHeight: 44,
    deviceRadio: 0,
  },
  onLaunch: function() {
    // 1、获取设备信息
    const info = wx.getSystemInfoSync()
    this.globalData.screenWidth = info.screenWidth
    this.globalData.screenHeight = info.screenHeight
    this.globalData.statusBarHeight = info.statusBarHeight

    const deviceRadio = info.screenHeight / info.screenWidth
    this.globalData.deviceRadio = deviceRadio

    // 2、让用户默认进行登录
    this.handleLogin()

    // 3、获取用户的信息
    
  },

  handleLogin: async function() {
    const token = wx.getStorageSync(TOKEN_KEY)
    // 判断token是否过期
    const checkResult = await checkToken()
    // 判断session是否过期
    const isSessionExpire = await checkSession()
    if (!token || checkResult.errorCode || !isSessionExpire) {
      this.loginAction()
    }
  },

  loginAction: async function() {
    // 1、获取code
    const code = await getLoginCode()
    
    // 将code发送给服务器
    const result = await sendCodeToServer(code)
    const token = result.token
    wx.setStorageSync(TOKEN_KEY, token)
  }
})
